﻿using System.Collections.Generic;
using System.IO;
using EnvDTE;
using EnvDTE80;
using System.Reflection;
using System;
using System.Diagnostics;

using Microsoft.VisualStudio.CommandBars;

using dotNails.UI;

namespace dotNails.Commands {
	/// <summary>
	/// This command opens the Configurator UI.
	/// </summary>
	public class OpenManagerCommand : CommandBase {
		#region Properties
		private MenuManager _menuManager;
		private CommandBarPopup _popupMenu;

		public MenuManager MenuManager {
			get { return _menuManager; }
			set { _menuManager = value; }
		}

		public CommandBarPopup PopupMenu {
			get { return _popupMenu; }
			set { _popupMenu = value; }
		}

		#endregion Properties

		#region Constructors
		public OpenManagerCommand(DTE2 application, EnvDTE.AddIn addInInstance, MenuManager menuManager, CommandBarPopup popupMenu)
			: base(application, addInInstance, "OpenManagerMenuCaption", "OpenManagerMenuTooltipText") {
			this.MenuManager = menuManager;
			this.PopupMenu = popupMenu;
		}
		#endregion Constructors

		/// <summary>
		/// Opens the Manager UI.
		/// </summary>
		public override void Perform() {
			UIHierarchy hierarchy = Application.ToolWindows.SolutionExplorer;

			//foreach (UIHierarchyItem selectedItem in (object[])hierarchy.SelectedItems)
			try {
				//Manager manager = new Manager(Application, AddInInstance, MenuManager, PopupMenu);
				//manager.Show();
				MainWindow mainWindow = new MainWindow(Application, AddInInstance, MenuManager, PopupMenu);

				int left = this.Application.MainWindow.Left;
				int top = this.Application.MainWindow.Top;
				int width = this.Application.MainWindow.Width;
				int height = this.Application.MainWindow.Height;

				mainWindow.Left = left + width / 2 - mainWindow.Width / 2;
				mainWindow.Top = top + height / 2 - mainWindow.Height / 2;
				
				mainWindow.Show();
			}
			catch (Exception ex) {
				Debug.WriteLine(ex.Message);
				Debug.WriteLine(ex.StackTrace);
			}
		}
	}
}
